﻿namespace Pps.Gui
{
    using Pps.Gui.Lcd;

    public static class WindowManager
    {
        private static object LockActiveWindow = new object();

        private static Display display = null;

        private static Window activeWindow = null;

        /// <summary>
        /// Gets the active window.
        /// </summary>
        /// <value>
        /// The active window.
        /// </value>
        public static Window ActiveWindow
        {
            get
            {
                return activeWindow;
            }
            private set
            {
                lock (LockActiveWindow)
                {
                    if (activeWindow != null)
                    {
                        display.TouchDown -= activeWindow.TouchDown;
                        display.TouchUp -= activeWindow.TouchUp;

                        activeWindow.DeactivateWindow();

                        //activeWindow.Dispose();
                    }

                    activeWindow = value;

                    activeWindow.Invalidate();

                    display.TouchDown += activeWindow.TouchDown;
                    display.TouchUp += activeWindow.TouchUp;

                    activeWindow.ActivateWindow();
                }
            }
        }

        /// <summary>
        /// Gets or sets the display.
        /// </summary>
        /// <value>
        /// The display.
        /// </value>
        public static Display Display
        {
            get { return display; }
            set { display = value; }
        }

        /// <summary>
        /// Changes the active window.
        /// </summary>
        /// <param name="window">The window.</param>
        public static void ChangeToWindow(Window window)
        {
            if (window != null && activeWindow != window)
            {
                WindowManager.ActiveWindow = window;
            }
        }
    }
}